package org.song.videoplayer;

import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.PopupDialog;
import ohos.agp.window.service.DisplayManager;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;
import java.util.List;

public class VideoPopWindow extends PopupDialog implements Component.ClickedListener {

    int h;

    public VideoPopWindow(Context context, ComponentContainer popview, List<QSVideo> qsVideos, int index) {
        super(context, popview);
        float density = DisplayManager.getInstance().getDefaultDisplay(context).get().getAttributes().densityPixels;
        int padding = (int) (density * 12);
        for (int i = 0; i < qsVideos.size(); i++) {
            QSVideo qsVideo = qsVideos.get(i);
            Text textView = new Text(context);
            textView.setId(i);
            textView.setPadding(padding, padding / 2, padding, padding / 2);
            textView.setText(qsVideo.resolution());
            textView.setTextSize(36);
            textView.setClickedListener(this);
            int colorMain = 0;
            try {
                colorMain = context.getResourceManager().getElement(ResourceTable.Color_colorMain).getColor();
            } catch (IOException | NotExistException | WrongTypeException e) {
                e.printStackTrace();
            }
            textView.setTextColor(index == i ? new Color(colorMain) : new Color(0xffffffff));
            popview.addComponent(textView);
        }
        int mode = Component.MeasureSpec.NOT_EXCEED;
        //手动调用计算宽高
        popview.estimateSize(Component.EstimateSpec.getSizeWithMode(1080, mode),
                Component.EstimateSpec.getSizeWithMode(1920, mode));
        h = popview.getEstimatedWidth();
        //设置视图
        setCustomComponent(popview);
        setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        setDialogListener(() -> {
            hide();
            return true;
        });
        setBackColor(Color.BLACK);
    }

    public void showTop(Component view) {
        int[] location = view.getLocationOnScreen();
        int offy = view.getEstimatedHeight() / 2 + 20;
        int offx = view.getEstimatedWidth();
        showOnCertainPosition(LayoutAlignment.TOP | LayoutAlignment.START, location[0]-offx, location[1] - offy - h);
    }

    private OnItemListener itemListener;

    public void setOnItemListener(OnItemListener itemListener) {
        this.itemListener = itemListener;
    }

    private OnDismissListener dismissListener;

    public void setOnDismissListener(OnDismissListener dismissListener) {
        this.dismissListener = dismissListener;
    }

    @Override
    public void onClick(Component component) {
        itemListener.OnClick(component.getId());
        hide();
    }

    public interface OnItemListener {
        void OnClick(int position);
    }

    public interface OnDismissListener {
        void onDismiss();
    }

    @Override
    protected void onHide() {
        super.onHide();
        dismissListener.onDismiss();
    }
}